/**
 * 
 */

import java.util.Date;
import java.util.GregorianCalendar;
import java.util.UUID;

/**
 * @author Andreas Waldherr (e1028787@student.tuwien.ac.at)
 *
 */
public enum CourseFactory {
	instance;
	
	private static void addDays(Date date, int days){
	    GregorianCalendar calendar = new GregorianCalendar();
	    calendar.setTime(date);
	    calendar.add(GregorianCalendar.DATE, days);
	    date.setTime(calendar.getTime().getTime());
	}
	
	/**
	 * This factory-method uses following dates:
	 *  registerStart is the current time.
	 *  registerEnd is in 7 days.
	 *  unregisterEnd is in 14 days.
	 * 
	 * @param courseID
	 * @param courseName
	 */
	public Course createCourse(String courseName){
		return createCourse(courseName, null);
	}
	
	/**
	 * This factory-method uses following dates:
	 *  registerEnd is 7 days after registerStart.
	 *  unregisterEnd is 7 days after registerEnd.
	 *  
	 * @param courseID
	 * @param courseName
	 * @param registerStart
	 */
	public Course createCourse(String courseName, Date registerStart){
		return createCourse(courseName, registerStart, null);
	}
	
	/**
	 * This factory-method uses following dates:
	 *  unregisterEnd is in 7 days after registerEnd.
	 *  
	 * @param courseID
	 * @param courseName
	 * @param registerStart
	 * @param registerEnd
	 */
	public Course createCourse(String courseName, Date registerStart, Date registerEnd){
		return  createCourse(courseName, registerStart, registerEnd, null);
	}
	
	/**
	 * @param courseID
	 * @param courseName
	 * @param registerStart current time, if null
	 * @param registerEnd registerStart plus 7 days if null
	 * @param unregisterEnd registerEnd plus 7 days if null
	 * @return A new course-object
	 */
	public Course createCourse(String courseName, Date registerStart, Date registerEnd, Date unregisterEnd){
		if(courseName == null || courseName.isEmpty()) {
			throw new IllegalArgumentException("courseName is not valid");
		}
		
		if(registerStart == null){
			registerStart = new Date();
		}
		
		if(registerEnd == null){
			registerEnd = new Date(registerStart.getTime());
			addDays(registerEnd, 7);
		}
		
		if(unregisterEnd == null){
			unregisterEnd = new Date(registerEnd.getTime());
			addDays(unregisterEnd, 7);
		}
		
		// claab edit start
		
		// generate unique id for course
		String courseID = UUID.randomUUID().toString();
		
		// check if dates are valid
		if(registerStart.after(registerEnd)) {
			throw new IllegalArgumentException("registerStart can not be after registerEnd");
		}
		if(unregisterEnd.before(registerEnd)) {
			throw new IllegalArgumentException("unregisterEnd can not be before registerEnd");
		}
		
		// claab edit end
		
		return new Course(courseID, courseName, registerStart, registerEnd, unregisterEnd);
	}
}
